/*
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
*/

String.metaClass.rotations = {
    s = delegate
    results = [s] as Set
    (s.length() - 1).times {
        s = s[1..-1] + s[0]
        results << s
    }
    results

}

file = new File('c:/dev/projecteuler/primes.txt')
primes = file.readLines()
startingAmount = primes.size()
primes = primes.findAll { !(it =~ /[024568]/) || it.size() == 1 }
println "Reduced primes to check from $startingAmount to ${primes.size()} total"

result = []
while (primes.size() > 0) {
    def r = primes[0].rotations()
    if (primes.containsAll(r)) {
        result += r
    }
    primes.removeAll(r)
    println "Remaining primes to check -> ${primes.size()}"
}

println "\nAnswer ==> ${result.size()}\n" + result*.toInteger().sort()





